package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.MallCartVO;
import com.ktgj.customer.domain.vo.WholeMallOrderVO;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.service.IMallOrderService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/MallOrder")
public class MallOrderController extends BaseController
{
    @Autowired
    private IMallOrderService mallOrderService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询订单列表
     */
    @PreAuthorize(hasPermi = "customer:order:list")
    @GetMapping("/list")
    public TableDataInfo list(MallOrder mallOrder)
    {
        startPage();
        List<MallOrder> list = mallOrderService.selectMallOrderList(mallOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize(hasPermi = "customer:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallOrder mallOrder) throws IOException
    {
        List<MallOrder> list = mallOrderService.selectMallOrderList(mallOrder);
        ExcelUtil<MallOrder> util = new ExcelUtil<MallOrder>(MallOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize(hasPermi = "customer:order:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(mallOrderService.selectMallOrderById(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize(hasPermi = "customer:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallOrder mallOrder)
    {
        return toAjax(mallOrderService.insertMallOrder(mallOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize(hasPermi = "customer:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallOrder mallOrder)
    {
        return toAjax(mallOrderService.updateMallOrder(mallOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize(hasPermi = "customer:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(mallOrderService.deleteMallOrderByIds(orderIds));
    }

    /**
     * 填写订单
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/fillOrder")
    public AjaxResult fillOrder(HttpServletRequest request, @RequestBody Map<String, List<Long>> map) {
        List<Long> productId = map.get("productId");
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (customer != null) {
            List<MallCartVO> mallCartVOS = null;
            if (customer.getOpenId() != null) {
                mallCartVOS = mallOrderService.fillOrder(customer.getCustomerId(), customer.getOpenId(), productId);
            } else {
                mallCartVOS = mallOrderService.fillOrder(customer.getCustomerId(), null, productId);
            }
            if (mallCartVOS != null && mallCartVOS.size() > 0)
                return AjaxResult.success(mallCartVOS);
            else
                return AjaxResult.success("暂无商品");
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * 生成订单
     * @param request
     * @param mallOrder
     * @return
     */
    @PostMapping("/createOrder")
    public AjaxResult createOrder(HttpServletRequest request, @RequestBody MallOrder mallOrder) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (customer != null) {
            String order = null;
            try {
                order = mallOrderService.createOrder(customer, mallOrder);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error(e.getMessage());
            }
            if (order != null)
                return AjaxResult.success(order);
            else
                return AjaxResult.error("生产订单失败");
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * 商城银行卡支付
     * @param bankCardPay
     * @return
     */
    @PostMapping("/bankCardPay")
    public AjaxResult bankCardPay(@RequestBody MallBankCardPay bankCardPay) {
        String cardPay;
        try {
            cardPay = mallOrderService.bankCardPay(bankCardPay);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(cardPay);
    }

    /**
     * 后台订单列表
     * @param search
     * @return
     */
    @PostMapping("/mallOrderList")
    public TableDataInfo mallOrderList(@RequestBody MallOrderSearch search) {
        startPage();
        List<MallOrderBackstage> mallOrderList = mallOrderService.mallOrderList(search);
        return getDataTable(mallOrderList);
    }

    /**
     * 后台订单详情
     * @param map
     * @return
     */
    @PostMapping("/orderBackstageDetails")
    public AjaxResult orderBackstageDetails(@RequestBody Map<String, Long> map) {
        Long orderId = map.get("orderId");
        MallOrderBackstageDetails backstageDetails = mallOrderService.orderBackstageDetails(orderId);
        return AjaxResult.success(backstageDetails);
    }

    /**
     * APP订单列表
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/wholeMallOrder")
    public AjaxResult wholeMallOrder(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (customer != null) {
            List<WholeMallOrder> wholeMallOrders = null;
            try {
                String status = map.get("status");
                wholeMallOrders = mallOrderService.wholeMallOrder(customer.getCustomerId(), status);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error(e.getMessage());
            }
            if (wholeMallOrders != null && wholeMallOrders.size() > 0)
                return AjaxResult.success(wholeMallOrders);
            else
                return AjaxResult.success("暂无订单");
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * APP订单详情
     * @param map
     * @return
     */
    @PostMapping("/mallOrderDetails")
    public AjaxResult mallOrderDetails(@RequestBody Map<String, Long> map) {
        Long orderId = map.get("orderId");
        WholeMallOrderVO mallOrderVO = mallOrderService.mallOrderDetails(orderId);
        return AjaxResult.success(mallOrderVO);
    }

    /**
     * 商城退单
     * @param map
     * @return
     */
    @PostMapping("/mallOrderRefund")
    public AjaxResult mallOrderRefund(@RequestBody Map<?, ?> map) {
        if (map.get("orderId") != null && !map.get("orderId").toString().equals("")) {
            Long orderId = Long.valueOf(map.get("orderId").toString());
            int i = mallOrderService.mallOrderRefund(orderId);
            return toAjax(i);
        } else {
            return AjaxResult.error("缺少必要参数！");
        }
    }
}
